package dot;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.jgrapht.Graph;
import org.jgrapht.graph.SimpleGraph;

import coloring.MyDefaultEdge;
import coloring.vertex.Vertex;

/**
 * Importing graph from .dot format to org.jgrapht.Graph
 * 
 * @author Croaker
 * 
 */
public class Import {

	/**
	 * Converting specified file.dot to org.jgrapht.Graph </br> </br><font
	 * color="red">WARNING!!!</font> there is couple of restrics for .dot
	 * file:</br></br> 1) EVERY node/edge on NEW string </br>2) NO special
	 * attributes</br> 3) Each string MUST be productive</br></br>
	 * 
	 * @param pathToFile
	 * @return
	 * @throws IOException
	 */
	public Graph<Vertex, MyDefaultEdge> genGraphFromDot(String pathToFile)
			throws IOException {
		boolean isSimpleVertices = true;
		boolean isSimpleEdges = true;
		if (!pathToFile.endsWith(".dot")) {
			System.out.println("Cannot recognize " + pathToFile
					+ " as .dot formatted file");
			throw new IOException();
		}

		Graph<Vertex, MyDefaultEdge> graph = new SimpleGraph<Vertex, MyDefaultEdge>(
				MyDefaultEdge.class);

		List<Vertex> list = new ArrayList<Vertex>();
		try {
			BufferedReader in = new BufferedReader(new FileReader(pathToFile));
			String str = in.readLine();
			while ((str = in.readLine()) != null) {
				if (str.startsWith("node [")) {
					System.out
							.println("General node description detected - file may be read with mistakes, so algorithm may fail");
				} else if (str.contains("--")) {
					String id1 = str.substring(0, str.indexOf("-"));
					id1 = id1.replaceAll(" ", "");
					int index = str.indexOf(";");
					if (str.indexOf("[") != -1) {
						index = str.indexOf("[");
						if (isSimpleEdges) {
							System.out
									.println("Dot file contains some additional information about edges that we wouldn't read");
							isSimpleEdges = false;
						}
					}
					String id2 = str.substring(str.indexOf("-") + 2, index);
					id2 = id2.replaceAll(" ", "");
					// System.out.println(id1 + "--" + id2);
					if (id1.equals(id2)) {
						System.out
								.println("Graph your specified shouldn't contains loops. Please, remove edge "
										+ id1 + " -- " + id2);
					}
					Vertex tmp1 = getVertNum(id1, list);
					Vertex tmp2 = getVertNum(id2, list);
					graph.addEdge(tmp1, tmp2);
				} else if (str.contains(";")) {
					int index = str.indexOf(";");
					if (str.indexOf("[") != -1) {
						index = str.indexOf("[");
						if (isSimpleVertices) {
							System.out
									.println("Dot file contains some additional information about vertices that we wouldn't read");
							isSimpleVertices = false;
						}
					}
					String id = str.substring(0, index);
					id = id.replaceAll(" ", "");
					Vertex tmp = new Vertex(id);
					list.add(tmp);
					graph.addVertex(tmp);
				}
			}
			in.close();
			if (!(isSimpleEdges && isSimpleVertices)) {
				System.out
						.println("File you specified isn't as simple as we want, so file may be read with mistakes, so algorithm may fail");
			}
			return graph;
		} catch (IOException e) {
			if (!(isSimpleEdges || isSimpleVertices)) {
				System.out
						.println("File you specified isn't as simple as we want, so file may be read with mistakes, so algorithm may fail");
			}
			System.out
					.println("Exception on reading from file, plese check it");
			e.printStackTrace();
			throw e;
		}
	}

	private Vertex getVertNum(String id, List<Vertex> list) {
		for (Vertex vertex : list) {
			if (vertex.getName().equals(id)) {
				return vertex;
			}
		}
		return null;
	}
}
